package com.ruoyi.mobile.project.pms.service;

import com.ruoyi.db.pms.domain.PmsGoodsSpecification;
import com.ruoyi.db.pms.mapper.PmsGoodsSpecificationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxPmsGoodsSpecificationService
{
    @Autowired
    private PmsGoodsSpecificationMapper goodsSpecificationMapper;

    /**
     * 根据商品id获取商品参数列表
     *
     * @author 张涛
     * @param id
     * @return
     */
    public List<PmsGoodsSpecification> queryByGid(Long id)
    {
        return goodsSpecificationMapper.selectPmsGoodsSpecificationBySid(id);
    }

    public PmsGoodsSpecification findById(Long id)
    {
        return null;
    }

    public void deleteByGid(Long gid)
    {

    }

    /**
     * 添加商品参数
     *
     * @author 张涛
     * @param goodsSpecification
     */
    public void add(PmsGoodsSpecification goodsSpecification)
    {
        goodsSpecification.setAddTime(LocalDateTime.now());
        goodsSpecification.setUpdateTime(LocalDateTime.now());
        goodsSpecificationMapper.insertPmsGoodsSpecification(goodsSpecification);
    }

    /**
     * 获取商品规格
     * @author 张涛
     * 将商品规格封装成以下数组的形式
     * [
     * {
     * name: '',
     * valueList: [ {}, {}]
     * },
     * {
     * name: '',
     * valueList: [ {}, {}]
     * }
     * ]
     *
     * @param id
     * @return
     */
    public Object getSpecificationVoList(Long id)
    {
        //获取商品规格数组
        List<PmsGoodsSpecification> goodsSpecificationList = queryByGid(id);

        Map<String, VO> map = new HashMap<>();
        List<VO> specificationVoList = new ArrayList<>();

        //遍历商品规格数组
        for (PmsGoodsSpecification goodsSpecification : goodsSpecificationList) {
            //获取规格名称
            String specification = goodsSpecification.getSpecification();
            //获取规格名称的键
            VO goodsSpecificationVo = map.get(specification);
            if (goodsSpecificationVo == null) {
                goodsSpecificationVo = new VO();
                //设置规格名称的参数
                goodsSpecificationVo.setName(specification);
                //定义一个动态数组保存名称对应的值
                List<PmsGoodsSpecification> valueList = new ArrayList<>();
                valueList.add(goodsSpecification);
                goodsSpecificationVo.setValueList(valueList);
                map.put(specification, goodsSpecificationVo);
                specificationVoList.add(goodsSpecificationVo);
            } else {
                List<PmsGoodsSpecification> valueList = goodsSpecificationVo.getValueList();
                valueList.add(goodsSpecification);
            }
        }

        return specificationVoList;
    }

    /**
     * 修改商品参数
     *
     * @author 张涛
     * @param specification
     */
    public void updateById(PmsGoodsSpecification specification)
    {
        specification.setUpdateTime(LocalDateTime.now());
        goodsSpecificationMapper.updatePmsGoodsSpecification(specification);
    }

    private class VO
    {
        private String name;
        private List<PmsGoodsSpecification> valueList;

        public String getName()
        {
            return name;
        }

        public void setName(String name)
        {
            this.name = name;
        }

        public List<PmsGoodsSpecification> getValueList()
        {
            return valueList;
        }

        public void setValueList(List<PmsGoodsSpecification> valueList)
        {
            this.valueList = valueList;
        }
    }

}
